import { BASE_URL } from '@/common/constants'
import { userStore } from "@/common/store";

const { getters } = userStore();

// get请求
export const getRequest = async (url, options = {}) => {
  const requestUrl = `${BASE_URL}${url}`;

  const defaultOptions = {
    method: "GET",
    headers: {
      ...options,
      'Content-Type': 'application/json',
      Author: getters.getIp(),
    },
  };

  try {
    const response = await fetch(requestUrl, defaultOptions);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error) {
    console.error("请求出现错误:", error);
    return null;
  }
};

// post请求
export const postRequest = async (url, params = {}, options = {}) => {
  const requestUrl = `${BASE_URL}${url}`;

  const defaultOptions = {
    method: "POST",
    body: JSON.stringify(params),
    headers: {
      ...options,
      'Content-Type': 'application/json',
      Author: getters.getIp(),
    },
  };

  try {
    const response = await fetch(requestUrl, defaultOptions);

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error) {
    console.error("请求出现错误:", error);
    return null;
  }
}

// sse请求
export const seeRequest = async (url, params = {}, options = {}) => {
  const requestUrl = `${BASE_URL}${url}`;

  const defaultOptions = {
    method: "POST",
    body: JSON.stringify(params),
    headers: {
      ...options,
      'Content-Type': 'application/json',
      Author: getters.getIp(),
    },
  };

  try {
    const response = await fetch(requestUrl, defaultOptions);

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.body;
    return data;
  } catch (error) {
    console.error("请求出现错误:", error);
    return null;
  }
}

// put请求
export const putRequest = async (url, params = {}, options = {}) => {
  const finalUrl = `${BASE_URL}${url}`;
  const defaultOptions = {
    method: 'PUT',
    headers: {
      ...options,
      'Content-Type': 'application/json',
      Author: getters.getIp(),
    },
    body: JSON.stringify(params),
  };

  try {
    const response = await fetch(finalUrl, defaultOptions);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const result = await response.json();
    return result;
  } catch (error) {
    console.error("PUT请求出现错误:", error);
    return null;
  }
};

// delete请求
export const deleteRequest = async (url, params, options = {}) => {
  let finalUrl = `${BASE_URL}${url}`;

  if (params) {
    finalUrl = `${finalUrl}/${params}`
  }

  const defaultOptions = {
    method: 'DELETE',
    headers: {
      ...options,
      'Content-Type': 'application/json',
      Author: getters.getIp(),
    },
  };

  try {
    const response = await fetch(finalUrl, defaultOptions);

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const result = await response.json();
    return result;
  } catch (error) {
    console.error("DELETE请求出现错误:", error);
    return null;
  }
}
